<?php
	use\ampleharvest\task;
	use\ampleharvest\code;

	require_once dirname(dirname(dirname(__FILE__))) . "/config/cConfig.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/count_DB_Docs.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/count_SOLR_Docs.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/scheduled_Queries.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/send_Email.php";
	//require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/metric/count_Source.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/metric/count_ActiveUsers.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/metric/count_TotalUsers.php";
	//require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/process_data/process_Data1.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/process_data/process_Data_Queue.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/tasks/index_data/index_Data_Queue.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/cThread.php";

	$str_ID = uniqid();

	$C_STR_TASK_COUNT_ACTIVE_USERS = "count_ActiveUsers";
	$C_STR_TASK_COUNT_DB_DOCS = "count_DB_Docs";
	$C_STR_TASK_COUNT_SOLR_DOCS = "count_SOLR_Docs";
	//$C_STR_TASK_COUNT_SOURCE = "count_Source";
	//$C_STR_TASK_PROCESS_DATA1 = "process_Data1";
	$C_STR_TASK_PROCESS_DATA_QUEUE = "process_Data_Queue";
	$C_STR_TASK_INDEX_DATA_QUEUE = "index_Data_Queue";
	$C_STR_TASK_SCHEDULED_QUERIES = "scheduled_Queries";
	$C_STR_TASK_SEND_EMAIL = "send_Email";
	$C_STR_TASK_COUNT_TOTAL_USERS = "count_TotalUsers";

	$str_LockPath = dirname(__FILE__);

	$arr_Threads = array();
	$arr_Tasks = array();
	
	
	$arr_Params = array(
		"thread::",
		"task::",
		"optimize::",
		"thread_id::"
	);
	
	$arr_Options = getopt("", $arr_Params);

	//Load Tasks into Array
	$arr_Tasks[$C_STR_TASK_COUNT_ACTIVE_USERS] = new task\metric\count_ActiveUsers($str_LockPath);
	$arr_Tasks[$C_STR_TASK_COUNT_DB_DOCS] = new task\count_DB_Docs($str_LockPath);
	$arr_Tasks[$C_STR_TASK_COUNT_SOLR_DOCS] = new task\count_SOLR_Docs($str_LockPath);
	//$arr_Tasks[$C_STR_TASK_COUNT_SOURCE] = new task\metric\count_Source($str_LockPath);
	//$arr_Tasks[$C_STR_TASK_PROCESS_DATA1] = new task\process_data\process_Data_1($str_LockPath);
	$arr_Tasks[$C_STR_TASK_PROCESS_DATA_QUEUE] = new task\process_data\process_Data_Queue($str_LockPath);
	$arr_Tasks[$C_STR_TASK_INDEX_DATA_QUEUE] = new task\index_data\index_Data_Queue($str_LockPath,$arr_Options);
	$arr_Tasks[$C_STR_TASK_SCHEDULED_QUERIES] = new task\scheduled_Queries($str_LockPath);
	$arr_Tasks[$C_STR_TASK_SEND_EMAIL] = new task\send_Email($str_LockPath);
	$arr_Tasks[$C_STR_TASK_COUNT_TOTAL_USERS] = new task\metric\count_TotalUsers($str_LockPath);


	$int_I = sizeof($arr_Threads);

	$arr_Threads[$int_I] = array();
	$arr_Threads[$int_I]["tasks"] = array();
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_PROCESS_DATA_QUEUE; //$arr_Tasks[$C_STR_TASK_PROCESS_DATA_QUEUE];
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_INDEX_DATA_QUEUE; //$arr_Tasks[$C_STR_TASK_INDEX_DATA_QUEUE];
	/*
	 $int_I = sizeof($arr_Threads);
	
	 $arr_Threads[$int_I] = array();
	 $arr_Threads[$int_I]["tasks"] = array();
	 $arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_PROCESS_DATA1; //$arr_Tasks[$C_STR_TASK_PROCESS_DATA1];
	 */
	$int_I = sizeof($arr_Threads);

	$arr_Threads[$int_I] = array();
	$arr_Threads[$int_I]["tasks"] = array();
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_COUNT_DB_DOCS; //$arr_Tasks[$C_STR_TASK_COUNT_DB_DOCS];
	//$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_COUNT_SOURCE; //$arr_Tasks[$C_STR_TASK_COUNT_SOURCE];

	$int_I = sizeof($arr_Threads);

	$arr_Threads[$int_I] = array();
	$arr_Threads[$int_I]["tasks"] = array();
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_COUNT_SOLR_DOCS; //$arr_Tasks[$C_STR_TASK_COUNT_SOLR_DOCS];

	$int_I = sizeof($arr_Threads);

	$arr_Threads[$int_I] = array();
	$arr_Threads[$int_I]["tasks"] = array();
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_COUNT_ACTIVE_USERS; //$arr_Tasks[$C_STR_TASK_COUNT_ACTIVE_USERS];
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_COUNT_TOTAL_USERS; //$arr_Tasks[$C_STR_TASK_COUNT_TOTAL_USERS];

	$int_I = sizeof($arr_Threads);

	$arr_Threads[$int_I] = array();
	$arr_Threads[$int_I]["tasks"] = array();
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_SCHEDULED_QUERIES; //$arr_Tasks[$C_STR_TASK_SCHEDULED_QUERIES];
	$arr_Threads[$int_I]["tasks"][] = $C_STR_TASK_SEND_EMAIL; //$arr_Tasks[$C_STR_TASK_SEND_EMAIL];


	if (isset($arr_Options["thread"]))
	{
		$arr_TaskThreads = array();
		foreach ($arr_Threads[$arr_Options["thread"]]["tasks"] as $obj_Task)
		{
			//print("Threads - " . sizeof($arr_TaskThreads) . PHP_EOL);
			//print("Max Threads - " . $arr_Tasks[$obj_Task]->get_MaxThreads() . PHP_EOL);

			$int_I = 0;
			while (sizeof($arr_TaskThreads) < $arr_Tasks[$obj_Task]->get_MaxThreads())
			{
				$int_I++;
				//print($str_ID . " - Thread Count" . sizeof($arr_TaskThreads) . PHP_EOL);
				//print($str_ID . " - Started a Thread" . PHP_EOL);
				$str_Command = __FILE__ . " --task=$obj_Task --thread_id=$int_I";
				print($str_Command . "\n");
				$arr_TaskThreads[$int_I] = cThread::Create($str_Command, code\cConfig::C_STR_PATH_PHP);
			}

			while (sizeof($arr_TaskThreads) > 0)
			{
				foreach ($arr_TaskThreads as $str_Key => $obj_Thread)
				{
					//print($obj_Thread->listen(false));
					if (!$obj_Thread->isActive())
					{
						print($obj_Thread->listen());
						$obj_Thread->close();
						//print($str_ID . " - Thread Closed" . PHP_EOL);
						unset($arr_TaskThreads[$str_Key]);
					}
				}
			}
		}
		exit(1);
	}

	if (isset($arr_Options["task"]))
	{
		if (isset($arr_Options["thread_id"]))
		{
			$arr_Tasks[$arr_Options["task"]]->set_ThreadID($arr_Options["thread_id"]);
		}
		$arr_Tasks[$arr_Options["task"]]->run();
		exit(1);
	}

	foreach ($arr_Threads as $str_Key => $arr_Thread)
	{
		$str_Command = __FILE__ . " --thread=$str_Key";
		print($str_Command . "\n");
		$arr_Threads[$str_Key]["thread"] = cThread::Create($str_Command, code\cConfig::C_STR_PATH_PHP);
	}

	while (sizeof($arr_Threads) > 0)
	{
		foreach ($arr_Threads as $str_Key => $obj_Thread)
		{
			print($obj_Thread["thread"]->listen(false));
			if (!$obj_Thread["thread"]->isActive())
			{
				print($obj_Thread["thread"]->listen());
				$obj_Thread["thread"]->close();
				unset($arr_Threads[$str_Key]);
			}
		}
	}

?>